<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Service
 * @version 		$Id: process.class.php 4977 2012-10-31 14:12:43Z Miguel_Espinoza $
 */
class Marketplace_Service_Store_Store extends Phpfox_Service {

    public function __construct() {
        $this->_sTable = Phpfox::getT("marketplace_store");
    }
    
    public function getStoreFromListing($iId){
        $aStore = $this->database()->select("store.*")
                ->from(Phpfox::getT('marketplace'),"m")
                ->join($this->_sTable,"store",'m.store_id = store.store_id')
                ->where("m.listing_id = ".$iId)
                ->execute("getRow");
        if(empty($aStore)){
            return false;
        }
        $sAddress = $aStore['address'];
        if(!empty($aStore["city"])){
            $sAddress .= " ".$aStore["city"];
        }
        if(!empty($aStore["country_child_id"])){
            $sAddress .= " ". Phpfox::getService("core.country")->getChild($aStore["country_child_id"]);
        }
        if(!empty($aStore["country_iso"])){
            $sAddress .= " ". Phpfox::getService("core.country")->getCountry($aStore["country_iso"]);
        }
        $aStore["full_address"] = $sAddress;
        return $aStore;
    }
    
    public function getProfileStoreLink($iUserId) {
        $aListing = $this->database()->select("*")
                ->from(Phpfox::getT("marketplace"))
                ->where("user_id = " . $iUserId)
                ->order("time_stamp DESC")
                ->limit(1)
                ->execute("getRow");
        if (empty($aListing))
            return false;
        return Phpfox::permalink('marketplace', $aListing["listing_id"], $aListing['title']);
    }

    public function getByUser($iUserId) {
        $aStores = $this->database()->select("*")
                ->from($this->_sTable)
                ->where("user_id = " . $iUserId)
                ->execute("getRows");

        return $aStores;
    }

    public function getForView($iId) {
        $aStore = $this->database()->select("*")
                ->from(Phpfox::getT("marketplace_store"))
                ->where("store_id = " . $iId)
                ->execute("getRow");
        return $aStore;
    }

    public function getForEdit($iId) {
        $aStore = $this->database()->select("*,address as store_address,name as store_name")
                ->from(Phpfox::getT("marketplace_store"))
                ->where("store_id = " . $iId)
                ->execute("getRow");
        if (!empty($aStore["link_data"])) {
            $aStore["links"] = json_decode($aStore["link_data"]);
        }
        return $aStore;
    }

    public function getRelatedProducts($aListing, $iLimit = 9) {
        if (!isset($aListing['listing_id'])) {
            return false;
        }
        if(empty($aListing["album_id"])){
            $aListing['album_id'] = 0;
            $aListing['album_title'] = "Default Album";
        }
        $aCategory = $aListing;
        if (empty($aCategory))
            return false;
        $aCategory["products"] = $this->database()->select("l.*")
                ->from(Phpfox::getT("marketplace"), "l")
                ->leftjoin(Phpfox::getT("marketplace_album"), "d", "d.album_id = l.album_id")
                ->where("l.album_id = " . $aCategory["album_id"] . " AND l.listing_id != " . $aListing['listing_id']." AND l.user_id = ".$aListing['user_id'])
                ->group("l.listing_id")
                ->order("l.time_stamp DESC")
                ->limit($iLimit)
                ->execute("getSlaveRows");
        if (count($aCategory["products"]) > 0) {
            foreach ($aCategory["products"] as $iKey => $aListing) {
                $aCategory["products"][$iKey]["url"] = Phpfox::permalink("marketplace", $aListing["listing_id"], $aListing["title"]);
            }
        }
        return $aCategory;
    }

    public function getOtherCategoryProducts($aListing, $iLimit = 6) {
        $aCategory = $this->database()->select("c.*")
                ->from(Phpfox::getT("marketplace_category"), "c")
                ->join(Phpfox::getT("marketplace_category_data"), "d", 'd.category_id = c.category_id')
                ->where("d.listing_id = " . $aListing['listing_id'])
                ->limit(1)
                ->execute("getSlaveRow");
        $aCategory = $this->database()->select("c.*")
                ->from(Phpfox::getT("marketplace_category"), "c")
                ->where("c.category_id != " . $aCategory['category_id'])
                ->limit(1)
                ->execute("getRow");
        if (empty($aCategory))
            return false;
        $aCategory["products"] = $this->database()->select("l.*")
                ->from(Phpfox::getT("marketplace"), "l")
                ->join(Phpfox::getT("marketplace_category_data"), "d", "d.listing_id = l.listing_id")
                ->where("d.category_id = " . $aCategory["category_id"])
                ->group("l.listing_id")
                ->order("l.time_stamp DESC")
                ->limit($iLimit)
                ->execute("getSlaveRows");
        if (count($aCategory["products"]) > 0) {
            foreach ($aCategory["products"] as $iKey => $aListing) {
                $aCategory["products"][$iKey]["url"] = Phpfox::permalink("marketplace", $aListing["listing_id"], $aListing["title"]);
            }
        }
        return $aCategory;
    }

    public function getOtherAlbumsProducts($aListing, $iLimit = 6) {
        if (!isset($aListing['listing_id']) || empty($aListing["album_id"])) {
            return false;
        }        
        $aAlbums = $this->database()->select("*")
                ->from(Phpfox::getT("marketplace_album"))
                ->where("user_id = ".$aListing["user_id"]." AND album_id != ".$aListing["album_id"])
                ->execute("getRows");
                
        if (empty($aAlbums))
            return false;
        foreach($aAlbums as $i  => $aAlbum){
            $aAlbums[$i]["products"] = $this->database()->select("l.*")
                    ->from(Phpfox::getT("marketplace"), "l")
                    ->where("l.album_id = " . $aAlbum["album_id"])
                    ->order("l.time_stamp DESC")
                    ->limit($iLimit)
                    ->execute("getSlaveRows");
            if (count($aAlbums[$i]["products"]) > 0) {
                foreach ($aAlbums[$i]["products"] as $iKey => $aListing) {
                    $aAlbums[$i]["products"][$iKey]["url"] = Phpfox::permalink("marketplace", $aListing["listing_id"], $aListing["title"]);
                }
            }
            else{
                unset($aAlbums[$i]);
            }
        }
        return $aAlbums;
    }

    public function suggestListings($iUserId, $iLimit = 10) {
        $aConds[] = "AND m.view_id = 0";
        $this->database()->select('mt.description_parsed AS description, ')->join(Phpfox::getT('marketplace_text'), 'mt', 'mt.listing_id = m.listing_id');
        $aListings = $this->database()->select('m.*, ' . Phpfox::getUserField())
                ->from(Phpfox::getT("marketplace"), 'm')
                ->join(Phpfox::getT('user'), 'u', 'u.user_id = m.user_id')
                ->where($aConds)
                ->order("m.time_stamp DESC")
                ->limit($iLimit)
                ->execute('getSlaveRows');
        if (!empty($aListings)) {
            Phpfox::getService("marketplace.browse")->processRows($aListings);
        }
        return $aListings;
    }

}

?>